package com.lry.basic.redis;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class PubSubApply {

    private Jedis jedis;

    @BeforeAll
    public void before() {
        jedis = RedisPool.getJedis();
    }

    @AfterAll
    public void after() {
        RedisPool.release(jedis);
    }


    @Test
    public void pub() throws InterruptedException {
        for(int i=0;i<100;i++){
            jedis.publish("sport",""+i);
            Thread.sleep(2000);
        }
    }

    @Test
    public void sub()  {
        jedis.subscribe(new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println(message);
            }
        },"sport");
    }
}
